package com.iterator.service.web.marketManager;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iterator.bean.web.lc.OrderBean.OrderDetailVO;
import com.iterator.bean.web.lc.OrderBean.OrderItem;
import com.iterator.bean.web.lc.OrderBean.OrderListBO;
import com.iterator.bean.web.lc.OrderBean.OrderListVO;
import com.iterator.bean.web.lc.OrderBean.OrderRefundBO;
import com.iterator.bean.web.lc.OrderBean.OrderShipBO;
import com.iterator.bean.web.pojo.Order;
import com.iterator.bean.web.pojo.OrderExample;
import com.iterator.mapper.web.marketManager.CustomOrderMapper;
import com.iterator.mapper.web.marketManager.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Repository
public class OrderServiceImpl implements OrderService{
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    CustomOrderMapper customOrderMapper;

    /**
     * 获取订单列表
     * @param listBO
     * @return
     */
    @Override
    public OrderListVO getList(OrderListBO listBO) {
        PageHelper.startPage(listBO.getPage(),listBO.getLimit());
        OrderExample example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        OrderExample.Criteria criteria1 = example.createCriteria();
        if(!StringUtils.isEmpty(listBO.getUserId())){
            criteria.andUserIdEqualTo(listBO.getUserId());
        }
        if(!StringUtils.isEmpty(listBO.getOrderSn())){
            criteria.andOrderSnEqualTo(listBO.getOrderSn().toString());
        }
        //or拼接订单状态数组
        if(!(listBO.getOrderStatusArray() == null || listBO.getOrderStatusArray().length==0 )){
            for (Integer orderStatus : listBO.getOrderStatusArray()) {
//     错误示范
//     criteria1.andOrderStatusEqualTo(Short.valueOf(orderStatus.toString()));
                example.or(example.createCriteria().andOrderStatusEqualTo
                        (Short.valueOf(orderStatus.toString())));

            }
        }


        List<Order> orders = orderMapper.selectByExample(example);
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        long total = pageInfo.getTotal();
        ArrayList<OrderItem> items = new ArrayList<>();
        for (Order order : orders) {
            if(!order.getDeleted()){
                items.add(new OrderItem(order.getId(),
                        order.getUserId(),
                        order.getOrderSn(),
                        order.getOrderStatus(),
                        order.getConsignee(),
                        order.getMobile(),
                        order.getAddress(),
                        order.getMessage(),
                        order.getGoodsPrice(),
                        order.getFreightPrice(),
                        order.getCouponPrice(),
                        order.getIntegralPrice(),
                        order.getGrouponPrice(),
                        order.getOrderPrice(),
                        order.getActualPrice(),
                        order.getComments(),
                        order.getAddTime(),
                        order.getUpdateTime(),
                        order.getDeleted()));
            }
        }
        OrderListVO orderListVO = new OrderListVO();
        orderListVO.setTotal((int) total);
        orderListVO.setItems(items);
        return orderListVO;
    }

    @Override
    public OrderDetailVO getDetail(Integer id) {
        OrderDetailVO orderDetailVO = customOrderMapper.selectByPrimaryKey(id);
        return  orderDetailVO;
    }

    @Override
    public Integer refund(OrderRefundBO refundBO) {
        Order order = new Order();
        order.setOrderStatus(203);
        OrderExample example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(refundBO.getOrderId());
        return orderMapper.updateByExampleSelective(order,example);
    }

    @Override
    public void ship(OrderShipBO shipBO) {
        Order order = new Order();
        order.setShipChannel(shipBO.getShipChannel());
        order.setShipSn(shipBO.getShipSn());
        order.setOrderStatus(301);
        OrderExample example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(shipBO.getOrderId());
        orderMapper.updateByExampleSelective(order,example);
    }


}
